{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "11aea1e4",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "from vnpy.trader.constant import Interval\n",
    "from vnpy.trader.object import AccountData, HistoryRequest\n",
    "from vnpy.trader.rqdata import rqdata_client\n",
    "from datetime import datetime\n",
    "from vnpy.trader.utility import extract_vt_symbol, generate_vt_symbol\n",
    "import traceback\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "79fb3e8b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "IF2109.CFFEX-Interval.MINUTE开始下载历史数据\n",
      "IF2109\n",
      "Exchange.CFFEX\n",
      "HistoryRequest(symbol='IF2109', exchange=<Exchange.CFFEX: 'CFFEX'>, start=datetime.datetime(2021, 8, 1, 0, 0), end=datetime.datetime(2021, 11, 1, 0, 0), interval=<Interval.MINUTE: '1m'>)\n",
      "数据下载失败，无法获取IF2109.CFFEX的历史数据\n"
     ]
    }
   ],
   "source": [
    "# IH8888\n",
    "# IF8888\n",
    "# IO8888\n",
    "# 2021-4-26 5001\n",
    "\n",
    "# vt_symbol = 'RB2105.SHFE'\n",
    "\n",
    "# vt_symbol = 'AU995.SGEX'\n",
    "vt_symbol = 'IF2109.CFFEX'\n",
    "# vt_symbol = 'AU2107.SHFE'\n",
    "# symbol = 'RB2105'\n",
    "# exchange = Exchange.SHFE\n",
    "# interval = Interval.DAILY\n",
    "# '00300.XFFSE'\n",
    "\n",
    "interval = Interval.MINUTE\n",
    "start = datetime(2021, 8, 1)\n",
    "end = datetime(2021, 11, 1)\n",
    "\n",
    "\"\"\"\n",
    "Query bar data from RQData.\n",
    "\"\"\"\n",
    "print(f\"{vt_symbol}-{interval}开始下载历史数据\")\n",
    "\n",
    "try:\n",
    "    symbol, exchange = extract_vt_symbol(vt_symbol)\n",
    "    print(symbol)\n",
    "    print(exchange)\n",
    "except Exception as e:\n",
    "    print(f\"{vt_symbol}解析失败，请检查交易所后缀\")\n",
    "    # self.thread = None\n",
    "    raise e\n",
    "    # return\n",
    "\n",
    "req = HistoryRequest(\n",
    "    symbol=symbol,\n",
    "    exchange=exchange,\n",
    "    interval=interval,\n",
    "    start=start,\n",
    "    end=end\n",
    ")\n",
    "try:\n",
    "    print(req)\n",
    "    rqdata_client.init('17767169181', 'Quant2021^')\n",
    "# end = datetime.now(get_localzone())\n",
    "# start = end - timedelta(30)\n",
    "    data = rqdata_client.query_history(req)\n",
    "\n",
    "    if data:\n",
    "        for item in data:\n",
    "            print(item)\n",
    "            print(item.interval)\n",
    "            item.interval = Interval(item.interval.value)\n",
    "        try:\n",
    "            database_manager.save_bar_data(data)\n",
    "        except Exception as e:\n",
    "            # raise e\n",
    "            msg = f\"数据下载失败，触发异常：\\n{traceback.format_exc()}\"\n",
    "            print(msg)\n",
    "\n",
    "        # for dataframe object using .tail(n=3)\n",
    "        print(f\"{vt_symbol}-{interval}历史数据下载完成\")\n",
    "    else:\n",
    "        print(f\"数据下载失败，无法获取{vt_symbol}的历史数据\")\n",
    "except Exception as e:\n",
    "    msg = f\"数据下载失败，触发异常：\\n{traceback.format_exc()}\"\n",
    "    print(msg)\n",
    "\n",
    "# Clear thread object handler.\n",
    "# self.thread = None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "068a70dc",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
